import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { RoleEntity } from '../entitys/role.entity'
var dayjs = require('dayjs')

@Injectable()
export class RoleService {
  constructor (
    @InjectRepository(RoleEntity)
    private readonly roleRepository: Repository<RoleEntity>
  ) { }

  async getRoleList(): Promise<any> {
    const sql = 'SELECT f_id as fId,f_name as fName, f_create_time as fCreateTime, f_state as fState,f_type as fType FROM t_role ORDER BY f_sort ASC'
    return await this.roleRepository.query(sql)
  }

  async addRole(addRoleDto): Promise<any> {
    const { fName, fSort } = addRoleDto
    const fCreateTime = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
    const sql = 'INSERT INTO t_role(f_name,f_sort,f_create_time,f_state,f_type) VALUE(?,?,?,0,1)'
    return await this.roleRepository.query(sql, [fName, fSort, fCreateTime])
  }

  async delRole(delRoleDto): Promise<any> {
    const { fId } = delRoleDto
    const sql = 'DELETE FROM t_role WHERE f_id = ?'
    return await this.roleRepository.query(sql, fId)
  }
}